package com.sks.web.action.privilege;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import com.sks.bean.pojo.Users;
import com.sks.bean.user.Gender;
import com.sks.service.privilege.UsersService;
import com.sks.utils.SiteUrl;
import com.sks.utils.WebUtil;
import com.sks.web.formbean.privilege.UsersForm;

@Controller("/sks/privilege/user/reg")
public class RegAction extends DispatchAction {

	@Resource
	UsersService usersService;

	/**
	 * 用户注册界面
	 */
	public ActionForward regUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;
		formbean.setSex(Gender.MAN);
		return mapping.findForward("reg");
	}

	/**
	 * 用户注册
	 */
	public ActionForward reg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;
		if (!usersService.exist(formbean.getLoginName())) {
			// 根据登录名、登录密码、邮箱新建一个用户
			Users users = new Users(formbean.getLoginName(), formbean
					.getLoginPwd(), formbean.getEmail());
			// users.setUserName(formbean.getUserName());
			// formbean.setSex(Gender.MAN);
			users.setFamilyName(formbean.getFamilyName());
			users.setSex(formbean.getSex()); // 性别在前台获取时出现类型不匹配问题。带解决
			users.setCardNo(formbean.getCardNo());
			users.setMobile(formbean.getMobile());
			users.setAddress(formbean.getAddress());
			Date date = new Date(); // 获取最新时间
			users.setRegisterTime(date);
			users.setLastUpdateTime(date);
			users.setUserType(1);
			users.setStatus(true);
			usersService.save(users);
			request.getSession().setAttribute("users", users);
			request.setAttribute("message", "用户注册成功");
			String url = "/sks/privilege/user/reg.do";
			// if(formbean.getDirectUrl()!=null)
			// url = formbean.getDirectUrl();
			request.setAttribute("urladdress", url);
			return mapping.findForward("regFinish");
		} else {
			request.setAttribute("message", "用户已经存在");
			return mapping.findForward("reg");
		}
	}

	/**
	 * 判断用户名是否存在
	 */
	public ActionForward isUserExsit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;
		request.setAttribute("exsit", usersService.exist(formbean
				.getLoginName()));
		return mapping.findForward("checkuser");
	}

	/**
	 * 网站用户信息修改界面请求
	 */
	public ActionForward editUsersUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;

		Users current = WebUtil.getUsers(request);
		Users users = usersService.find(current.getUserId());
		if (null == users
				|| !users.getLoginName().equals(current.getLoginName())) {
			request.setAttribute("forwardTo",
					"/sks/frontpages/privilege/deployment/list.do");
			return mapping.findForward("noUser");
		}

		formbean.setFamilyName(users.getFamilyName());
		formbean.setSex(users.getSex());
		formbean.setEmail(users.getEmail());
		formbean.setUserId(users.getUserId());
		formbean.setLoginName(users.getLoginName());
		formbean.setMobile(users.getMobile());
		formbean.setStatus(users.getStatus());
		formbean.setCardNo(users.getCardNo());
		// request.setAttribute("imagePath", employee.getImagePath());
		// request.setAttribute("departments",
		// departmentService.getScrollData().getResultlist());
		return mapping.findForward("edit");
	}

	/**
	 * 网站用户信息修改
	 */
	public ActionForward editUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		boolean success = false;
		UsersForm formbean = (UsersForm) form;
		Users current = WebUtil.getUsers(request);
		Users users = usersService.find(current.getUserId());
		if (null == users
				|| !users.getLoginName().equals(current.getLoginName())) {
			request.setAttribute("forwardTo",
					"/sks/frontpages/privilege/deployment/list.do");
			return mapping.findForward("noUser");
		}
		users.setSex(formbean.getSex());
		users.setEmail(formbean.getEmail());
		users.setLoginName(formbean.getLoginName());
		users.setMobile(formbean.getMobile());
		users.setFamilyName(formbean.getFamilyName());
		users.setCardNo(formbean.getCardNo());
		try{
			usersService.update(users);
			success = true;
		}catch(Exception e){
			success = false;
		}
		request.setAttribute("message", "外部用户修改成功");
	//	request.setAttribute("urladdress", SiteUrl
	//			.readUrl("sks.privilege.users.list"));
		
		response.getWriter().print("{\"success\":" + success + "}");
		//return editUsersUI(mapping, form, request, response);
		return null;
	}
}
